#include <exception>
#include <string.h>
#include <iostream>
using namespace std;
class array
{
    private:
        int position;
        int length;
        int* data;
    public:
        array(int size = 10);
        array(const array &ayy);
        ~array();
        int size();
        bool empty();
        int capacity();
        void setposition(int position);
        int getposition();
        // 获取数据
        int at(int index);
        // 插入数据
        void insert(int pos,int data);
        // 删除数据
        void remove(int pos);
        // 展示数组
        void show();
};
array::array(int size):position(0),length(size),data(new int[length]){}
array::array(const array&arr):position(arr.position),length(arr.length),data(new int[length])
{
    memcpy(data,arr.data,sizeof(int) *length);
}
array::~array(){}
int array::size()
{
    return length;
}
int array::capacity()
{
    return position;
}
void array::show()
{
    cout <<"当前数组为：";
    for(int i=0;i<position;i++)
        cout<<data[i]<<" ";
    cout<<endl;
}
bool array::empty()
{
    return position == 0 ? true:false;
}
int array::at(int index)
{
    if(index <0 || index >= length)
        throw out_of_range("index out of range.");
    else
        return data[index];
}
void array::insert(int index,int value)
{
    if ( position==length )
    {
        int *temp = new int [length*2];
        for ( int i=0;i<index;i++ )
            temp[i] = data[i];
        temp[index] = value;
        for ( int j=index;j<=position;j++ )
            temp[j+1] = data[j];
        length *= 2;
        position++;
        delete[] data;
        data = temp;
    }
    if ( position>=index )
    {
        for ( int j=position;j>index;j-- )
            data[j] = data[j-1];
        data[index] = value;        
        position++;
    }
    if( position<index )
        throw out_of_range("index out of range.");
}
void remove(int pos)
{

}
int main()
{
    array arr;
    arr.insert(0,0);arr.insert(1,1);arr.insert(2,2);arr.insert(3,3);
    arr.show();
    int index;
    cout << "选择查看的位置：" ;cin >> index;
    cout << "当前位置数组值为："<<arr.at(index);
}

